﻿package fwk.form {
	import flash.events.FocusEvent;
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import fwk.form.elements.AbstractQuestion;
	import fwk.form.elements.QuestionComboBox;
	import fwk.form.elements.QuestionDateText;
	import fwk.form.elements.QuestionRadioCheck;
	import fwk.form.elements.QuestionText;
	import fwk.form.elements.AbstractReponse;
	import fwk.form.CheckFormElements;
	

	public class FormManager 
	{
		
		private var _tabQuestions:Array;
		private var _containerQuestionsClip:Sprite;
		
		private var _buttonConfirm:Sprite;
		
		private var _tabulation:Boolean = false;
		private var _rollOver:Boolean;
		private var _shapeFocus:Boolean = false;
		private var _colorShapeFocus:int;
		
		private var _stage:Stage;
		private var _spriteFocus : Sprite;

		
		public function FormManager() 
		{
			_tabQuestions = new Array();
				
		}
		
		/**
		 * inititialise le comportement du formulaire
		 * @param  containerQuestionsClip est le container des clips question formater comme il faut, le nom du clip doit être de la forme "question_idquestion"
		 * @param  xmlForm est le noeud xml qui contient la description des questions
		 */
		public function init(containerQuestionsClip:Sprite, xmlForm:XML , tabulation:Boolean = false, pStage:Stage = null)
		{
	
			if (pStage != null) {
				_stage = pStage;
				pStage.stageFocusRect = false;
			}
			
			_containerQuestionsClip = containerQuestionsClip;
			
			
			//_containerQuestionsClip.addEventListener(Event.ADDED_TO_STAGE, formAddedToStage);
			_containerQuestionsClip.addEventListener(Event.REMOVED_FROM_STAGE, formRemoveFromStage);
			
			
			trace("xmlForm.input.length() " + xmlForm.children().length());
			for (var i:int = 0; i < xmlForm.children().length(); i++)
			{
				
				var clipQuestion:MovieClip = _containerQuestionsClip.getChildByName("question_" + xmlForm.children()[i].@id) as MovieClip;
				
				switch( String(xmlForm.children()[i].@type) ) {
					
					case "text": 
						var questionText:QuestionText = new QuestionText(clipQuestion);
						questionText.init(xmlForm.children()[i], clipQuestion.input.tTexte);
						_tabQuestions.push(questionText);
					break;
					case "dateText": 
						var questionDate:QuestionDateText = new QuestionDateText(clipQuestion);
						questionDate.init(xmlForm.children()[i], clipQuestion.input.day.tTexte, clipQuestion.input.month.tTexte, clipQuestion.input.year.tTexte);
						_tabQuestions.push(questionDate);
					break;
					case "radioButton": 
						initRadioCheck("radioButton", clipQuestion, xmlForm.children()[i]);
					break;
					case "checkBox": 
						trace(" xmlForm.children()[i] " +  xmlForm.children()[i]);
						initRadioCheck("checkBox", clipQuestion, xmlForm.children()[i]);
					break;
					case "comboBox":
						initComboBox(clipQuestion, xmlForm.children()[i]);
					break;
				}
				
			}
			
			if ( tabulation) tabulation = true;
			rollOver = false;
			
				
		}
		
		private function formRemoveFromStage(e:Event):void 
		{
			trace("formRemoveFromStage");
			
			if( _tabulation) tabulation = false;
		}
		
		private function formAddedToStage(e:Event):void 
		{
		
			trace("formAddedToStage");
			
			if( !_tabulation)   tabulation = true;
			
		}
		/**
		 * actif ou desactif la tabulation
		 */
		public function set tabulation(val:Boolean):void
		{

			_tabulation = val;
			

			
			var index:int = 1;
			for (var i:int = 0; i < _tabQuestions.length; i++) 
			{
				var question:AbstractQuestion = _tabQuestions[i];
				
				
				if ( question is QuestionRadioCheck) {
					for (var j:int = 0; j < question.reponsesTab.length; j++) 
					{
						var rep:AbstractReponse = question.reponsesTab[j];
						_tabulation ? rep.tabIndex = index : rep.tabIndex = -1;
						index ++;
					}
					
				}else {
					_tabulation ? question.tabIndex = index : question.tabIndex = -1;
					index++;
				}
				
				
			}
			

			// button Confirm
			if( _buttonConfirm != null){
				_tabulation ? _buttonConfirm.tabIndex = index : _buttonConfirm.tabIndex = -1;
			}

			 
			
			
		}
		/**
		 * ferme le formulaire
		 */
		public function close():void
		{
			tabulation = false;
				
		}
		/**
		 * retourne si tabluation active ou non
		 */
		public function get tabulation():Boolean
		{
			return _tabulation;
		}
		
		public function get rollOver():Boolean { return _rollOver; }
		
		public function set rollOver(value:Boolean):void 
		{
			_rollOver = value;
			
			for (var i:int = 0; i < _tabQuestions.length; i++) 
			{
				var question:AbstractQuestion = _tabQuestions[i];
				

				if (question is QuestionRadioCheck)
				{
					for (var j:int = 0; j < question.reponsesTab.length; j++) 
					{
						var reponse:AbstractReponse = question.reponsesTab[j];
						reponse.rollOver = _rollOver;
						
					}
				}else {
					question.rollOver = _rollOver;
				}
				
			}
			
		}
		
		public function get colorShapeFocus():int { return _colorShapeFocus; }
		
		public function set colorShapeFocus(value:int):void 
		{
			_colorShapeFocus = value;
			for (var i:int = 0; i < _tabQuestions.length; i++) 
			{
				var question:AbstractQuestion = _tabQuestions[i];
				
				
				if (question is QuestionRadioCheck)
				{
					for (var j:int = 0; j < question.reponsesTab.length; j++) 
					{
						var reponse:AbstractReponse = question.reponsesTab[j];
						reponse.colorShapeFocus = _colorShapeFocus;
						
					}
				}else {
					question.colorShapeFocus = _colorShapeFocus;
				}
				

			}
			
			// button confirm
			if( _buttonConfirm != null)
			{
				_spriteFocus.graphics.clear();
				_spriteFocus.graphics.clear();
				_spriteFocus.graphics.beginFill(_colorShapeFocus, 0.2);
				_spriteFocus.graphics.drawRect( -3,  - 3, _buttonConfirm.width + 6, _buttonConfirm.height + 6);
				
			}
			
			
		}
		
		public function get shapeFocus():Boolean { return _shapeFocus; }
		
		public function set shapeFocus(value:Boolean):void 
		{
			_shapeFocus = value;
			for (var i:int = 0; i < _tabQuestions.length; i++) 
			{
				var question:AbstractQuestion = _tabQuestions[i];
				
				
				if (question is QuestionRadioCheck)
				{
					for (var j:int = 0; j < question.reponsesTab.length; j++) 
					{
						var reponse:AbstractReponse = question.reponsesTab[j];
						reponse.shapeFocus = _shapeFocus;
						
					}
				}else {
					question.shapeFocus = _shapeFocus;
				}
				

			}
		}

		public function set buttonConfirm(val:Sprite) : void {
			
			if( _buttonConfirm == null)
			{
				_buttonConfirm = val;
				
				_spriteFocus = new Sprite();
				_spriteFocus.graphics.clear();
				_spriteFocus.graphics.beginFill(_colorShapeFocus, 0.2);
				_spriteFocus.graphics.drawRect( -3,  - 3, _buttonConfirm.width + 6, _buttonConfirm.height + 6);
				_buttonConfirm.addChildAt(_spriteFocus, 0);
				_spriteFocus.visible = false;
				
				
				_buttonConfirm.addEventListener(FocusEvent.FOCUS_IN, onFocusInButtonConfirm);
				_buttonConfirm.addEventListener(FocusEvent.FOCUS_OUT, onFocusOutButtonConfirm);
			}
		}

		private function onFocusOutButtonConfirm(event : FocusEvent) : void {
			if( _shapeFocus) _spriteFocus.visible = false;
		}

		private function onFocusInButtonConfirm(event : FocusEvent) : void {
			if( _shapeFocus) _spriteFocus.visible = true;
		}

		
		/**
		 * Verifie le formulaire et renvoie un tableau d'id d'erreurs dans l'ordre rencontrées
		 */
		public function check():Array
		{
			trace("check");
			trace("_tabQuestions.length " + _tabQuestions.length);
			return CheckFormElements.check(_tabQuestions);
		}
		/**
		 * Reset le formulaire
		 */
		public function reset():void
		{
			for (var i:int = 0; i < _tabQuestions.length; i++) 
			{
				var q:AbstractQuestion = _tabQuestions[i];
				q.reset();
				
			}
		}
		/**
		 * Renvoi l'object AbstractQuestion correspondant à l'id passé en paramètre
		 */
		public function getQuestion(id:String):AbstractQuestion
		{

			for each(var p:AbstractQuestion in _tabQuestions) {
				
				if (p.id == id) return p;
			}
			
			return null;
			
		}
		function initRadioCheck(type:String, clipQuestion:MovieClip, xmlQuestion:XML) {
			
			trace("initRadioCheck");
			trace("xmlQuestion.children().length() "  +xmlQuestion);
			trace("type "  + type);
			
			var question:QuestionRadioCheck = new QuestionRadioCheck(clipQuestion);
			question.init(xmlQuestion);
			_tabQuestions.push(question);
			

		}
		function initComboBox(clipQuestion:MovieClip, xmlQuestion:XML) {
			
			trace("initComboBox");
			trace("xmlQuestion.children().length() "  +xmlQuestion);
			
			var question:QuestionComboBox = new QuestionComboBox(clipQuestion, _stage);
			question.init(xmlQuestion);
			_tabQuestions.push(question);
			

		}
		
	}
	
}